package allwe.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CuratorConfig {
    /**
     * 重试次数
     */
    private int retryCount = 5;

    /**
     * 重试间隔时间
     */
    private int elapsedTimeMS = 5000;

    /**
     * 连接地址
     */
    private String connectAddress = "39.107.66.31:2181";

    /**
     * 会话超时时间
     */
    private int sessionTimeoutMS = 60000;

    /**
     * 连接超时时间
     */
    private int connectTimeMS = 5000;

    @Bean
    public CuratorFramework curatorFramework() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(elapsedTimeMS, retryCount);

        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(
                        connectAddress,
                        sessionTimeoutMS,
                        connectTimeMS,
                        retryPolicy);

        curatorFramework.start();
        return curatorFramework;
    }

}
